const { join } = require("path")
const { resolve } = require("path")
const webpack = require("webpack")

const apiEnv = {
  pro: "https://temp.3dyunzhan.com"
}

module.exports = [
  {
    mode: "development",
    entry: `./src/index.ts`,
    output: {
      publicPath: "/dist/",
      path: resolve(__dirname, "dist"),
      filename: `showcase.js`,
      globalObject: "self",
      chunkFilename: "[name].js"
    },
    resolve: {
      extensions: [".tsx", ".ts", ".js"]
    },
    resolveLoader: {
      moduleExtensions: ["-loader"]
    },
    module: {
      rules: [
        {
          test: /\.ts(x)?|\.js$/,
          loader: "esbuild",
          options: {
            loader: "ts",
            target: "es2015"
          }
        }
      ]
    },
    devtool: "source-map",
    externals: {
      three: "THREE"
    },
    devServer: {
      compress: true,
      static: {
        directory: join(__dirname, "./")
      },
      watchFiles: ["src/**/*.ts"],
      proxy: {
        // "/cdn": {
        //   target: "https://cdn-1.matterportvr.cn",
        //   pathRewrite: { "^/cdn": "" }
        // }
      }
    },
    watchOptions: {
      aggregateTimeout: 3000,
      ignored: ["files/**/*.js", "node_modules"],
      poll: 2000
    },
    plugins: [
      new webpack.DefinePlugin({
        "process.env": {
          NODE_ENV: JSON.stringify(process.env.NODE_ENV),
          API_ENV: JSON.stringify(apiEnv[process.env.API_ENV || "pro"])
        }
      })
    ]
  },
  {
    mode: "development",
    entry: `./src/client/index.ts`,
    output: {
      publicPath: "/dist/",
      path: resolve(__dirname, "dist"),
      filename: `sdk-client.js`,
      globalObject: "self",
      chunkFilename: "[name].js"
    },
    resolve: {
      extensions: [".tsx", ".ts", ".js"]
    },
    resolveLoader: {
      moduleExtensions: ["-loader"]
    },
    module: {
      rules: [
        {
          test: /\.ts(x)?|\.js$/,
          loader: "esbuild",
          options: {
            loader: "ts",
            target: "es2015"
          }
        }
      ]
    },
    devtool: "source-map",
    externals: {
      three: "THREE"
    }
  }
]
